package com.wpl.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.wpl.oss.service.OssService;
import com.wpl.oss.utils.Properties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Service
public class OssServiceImpl implements OssService {
    @Override
    public String uploadFiles(MultipartFile file) throws IOException {
        String endpoint = Properties.END_PONIT;
        String bucket = Properties.BUCKET_NAME;
        String keyId = Properties.KEY_ID;
        String keySecret = Properties.KEY_SECRET;

        OSS ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret);
        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        ossClient.putObject(bucket, originalFilename, inputStream);
        ossClient.shutdown();
        String url = "https://" + bucket + "." + endpoint + "/" + originalFilename;
        return url;
    }
}
